package py.org.atom.seam;

import javax.persistence.EntityManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.FlushModeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.international.Messages;
import org.jboss.seam.log.Log;

import py.org.atom.seam.util.ContextUtil;


public class BaseSIUD<E> extends Base{
	
    @In(scope=ScopeType.SESSION) 
    protected Identity identity;
    
    @In(create = true)
	protected EntityManager entityManager;    
    
    @Logger 
    protected Log log;
    
    protected String action = "list";    
    
    protected boolean loadedInstance=false;
	
    protected E instance;
	
    protected Integer cid;
    
	protected String _name;
	
	@Begin(join=true)
	public Integer getCid(){
		Conversation c = Conversation.instance();
		ContextUtil.clean(this._name);
		c.changeFlushMode(FlushModeType.MANUAL);
		this.cid = Integer.parseInt(c.getId());
		return this.cid;
	}
	
	
	public void setCid(Integer cid){
		this.cid = cid;
	}    
    
	public void setInstance(E instance){
		this.instance = instance;
	}
	
	public E getInstance(){
		return this.instance;
	}
	
	
	public boolean persist(E instance){
		this.instance = instance;
		return this.persist();
	}
	
	public boolean persist(){
		
		if(!validPersist())
			return false;
		
		try{
			this.entityManager.persist(this.instance);
			this.entityManager.flush();
			this.instance = null;
		}catch(Exception e){
			String msg = Messages.instance().get("py.org.atom.error.Internal");
			this.error(msg + " " + e);
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public boolean remove(E instance){
		this.instance = instance;
		return this.remove();
	}	
	
	public boolean remove(){
		if(!validRemove())
			return false;
		try{
			this.entityManager.remove(this.instance);
			this.entityManager.flush();
			this.instance = null;
			this.loadedInstance = false;
		}catch(Exception e){
			String msg = Messages.instance().get("py.org.atom.error.Internal");
			this.error(msg + " " + e);
			e.printStackTrace();
			return false;
		}
		
		return true;		
	}
	
	public boolean update(E instance){
		this.instance = instance;
		return this.update();
	}	
	
	public boolean update(){
		if(!validUpdate())
			return false;
		try{
			this.entityManager.merge(this.instance);
			this.entityManager.flush();
		}catch(Exception e){
			String msg = Messages.instance().get("py.org.atom.error.Internal");
			this.error(msg + " " + e);
			e.printStackTrace();
			return false;
		}
		
		return true;		
	}	
	
	public boolean validPersist(){
		if(this.instance == null)
			return false;
		return true;
	}
	public boolean validRemove(){
		if(this.instance == null)
			return false;
		return true;
	}
	public boolean validUpdate(){
		if(this.instance == null)
			return false;
		return true;
	}
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}
	
	public void goTo(String action) {
		this.action = action;
	}	
    
	protected String getUsr(){
		
		if(this.identity != null 
				&& this.identity.getCredentials() != null 
				&& this.identity.getCredentials().getUsername() != null)
			return this.identity.getCredentials().getUsername().trim();
		
		return "";
		
	}

	public boolean isLoadedInstance() {
		return loadedInstance;
	}

	public void setLoadedInstance(boolean loadedInstance) {
		this.loadedInstance = loadedInstance;
	}
	
	
}
